package com.helen.ui.order;

import android.content.Context;
import android.content.Intent;

import com.helen.db.UserHelper;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.utils.JsonUtil;
import com.helen.utils.tiputils.MProgressDialog;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

/**
 * Created by yang on 2018/6/3.
 */

public class HandleOrderStatus {

    private final MProgressDialog mProgressDialog;
    private Intent intent;
    private NavToOrderActivityListener navToOrderActivityListener;

    public HandleOrderStatus(Context context) {

        mProgressDialog = new MProgressDialog.Builder(context)
                .isCanceledOnTouchOutside(true)
                .setOnDialogDismissListener(new MProgressDialog.OnDialogDismissListener() {
                    @Override
                    public void dismiss() {

                    }
                })
                .build();
    }


    //   7  取消订单， 6 删除订单,  5 确认收货
    public void closeOrderApi(final Context context, final int type, String sid) {
        mProgressDialog.show();
        String url = "";
        switch (type) {
            case 7:
                url = Constants.API_JSONAPI_CLOSEORDER;
                break;
            case 6:
                url = Constants.API_JSONAPI_DELETEORDER;
                break;
            case 5:
                url = Constants.API_JSONAPI_CONFIRMORDER;
                break;
        }
        HttpParams params = new HttpParams();
        params.put("id", UserHelper.getInstance().getloginEntity(context).getId() + "");
        params.put("sid", sid);
        EasyHttp.post(url)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MToast.makeTextShort(context, context.getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String result) {
                        int code = JsonUtil.getJsonValuesInt(result, "code");
                        String msg = JsonUtil.getJsonValuesString(result, "msg");
                        if (code == 0) {
                            switch (type) {
                                case 7:

                                    intent = new Intent();
                                    intent.setAction(OrderActivity.orderAction);
                                    intent.putExtra("currentId", 0);
                                    context.sendBroadcast(intent);
                                    MToast.makeTextShort(context, "取消成功").show();
                                    if (context instanceof OrderDetailActivity) {
                                         if(navToOrderActivityListener!=null){
                                             navToOrderActivityListener.setOnNavToOrderActivityListener();
                                         }
                                    }
                                    break;
                                case 6:
                                    intent = new Intent();
                                    intent.setAction(OrderActivity.orderAction);
                                    intent.putExtra("currentId", 0);
                                    context.sendBroadcast(intent);
                                    MToast.makeTextShort(context, "删除成功").show();
                                    if (context instanceof OrderDetailActivity) {
                                        navToOrderActivityListener.setOnNavToOrderActivityListener();
                                    }
                                    break;
                                case 5:
                                    intent = new Intent();
                                    intent.setAction(OrderActivity.orderAction);
                                    intent.putExtra("currentId", 4);
                                    context.sendBroadcast(intent);
                                    if (context instanceof OrderDetailActivity) {
                                        navToOrderActivityListener.setOnNavToOrderActivityListener();
                                    }
                                    break;
                            }

                        } else {
                            MToast.makeTextShort(context, msg).show();
                        }

                    }
                });
    }

    public interface NavToOrderActivityListener {
        void setOnNavToOrderActivityListener();
    }


    public void setOnNavToOrderActivity(NavToOrderActivityListener navToOrderActivityListener) {
        this.navToOrderActivityListener = navToOrderActivityListener;
    }
}
